package com.dinstar.dinlink.dialog

import android.content.DialogInterface
import android.graphics.Color
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.util.DisplayMetrics
import android.view.*
import android.view.animation.AnimationUtils
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.dinstar.dinlink.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


/**
 *@author tangshixiao
 *@description
 *@date 2024/2/24 10:53
 */
class MessagePermissionsDialog(
    private val title: String? = null,
    private val message: SpannableStringBuilder? = null,
    private val cancelable: Boolean = false,
    private val alignment: Int = Gravity.TOP,
) : DialogFragment() {
    constructor(
        title: String? = null,
        message: String? = null,
        cancelable: Boolean = false,
        alignment: Int = Gravity.TOP,
    ) : this(
        title,
        SpannableStringBuilder(message),
        cancelable,
        alignment,
    )

    companion object {
        val TAG: String = MessagePermissionsDialog::class.java.simpleName
    }

    private lateinit var mParentView: View

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.MessageNoticeDialog)
        val enterAnimation = AnimationUtils.loadAnimation(
            context, R.anim.fade_in
        )
    }

//    private var job: Job? = null

    override fun onStart() {
        super.onStart()
        val window = dialog!!.window
        val wlp = window!!.attributes
        val dm = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(dm)
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        wlp.gravity = alignment
        wlp.width =dm.widthPixels
        wlp?.windowAnimations = R.anim.fade_in; //设置底部动画
        window.attributes = wlp
    }

    override fun onStop() {
        super.onStop()
//        job?.cancel()
    }



    //直接调用onDismiss，整个弹框会被销毁，再次调用show会重新走生命周期
//    @Override
//    public void onDismiss(@NonNull DialogInterface dialog) {
//        //仅调用hide，不销毁，仅隐藏
//        getDialog().hide();
//    }


    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        //仅调用hide，不销毁，仅隐藏
        getDialog()?.hide();
    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        mParentView = layoutInflater.inflate(R.layout.dialog_message_permiss, container, false)

        return mParentView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        dialog?.setCancelable(cancelable)
        dialog?.setCanceledOnTouchOutside(cancelable)

        mParentView.findViewById<TextView>(R.id.tv_message_p).apply {
            text = message
            gravity = alignment
            highlightColor = Color.TRANSPARENT
            movementMethod = LinkMovementMethod.getInstance()
        }
        mParentView.findViewById<TextView>(R.id.tv_title_p).text = title
    }

    fun show(fragmentManager: FragmentManager) {
        if (fragmentManager.findFragmentByTag(TAG)?.isVisible == true) {
            return
        }
        this.show(fragmentManager, TAG)
    }
}


